package view;

import model.AcolyteDeck;
import model.AcolyteCard;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.JFileChooser;
import java.awt.GridLayout;
import java.util.LinkedList;
import java.awt.event.ActionListener;
import javax.swing.event.MenuListener;
import javax.swing.event.MenuEvent;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

public class DeckEditor extends JFrame implements ActionListener
{
  
  private AcolyteDeck currentDeck;
  private JPanel cardPanel, buttonPanel, deckPanel;
  private java.awt.List cardList, deckList;
  private CardTemplate template;
  
  public DeckEditor()
  {
    super("new deck");
    //this.addActionListener(this);
    
    this.currentDeck = new AcolyteDeck();
    setUpAndShow();
  }
  
  DeckEditor(String filename)
  {
    super(filename);
    //this.addActionListener(this);
    
    this.currentDeck = new AcolyteDeck(filename);
    setUpAndShow();
  }

  private void setUpAndShow()
  {
    template = new CardTemplate("templateExample.xml");
    //super(filename);
    //this.addActionListener(this);
    
    //this.currentDeck = new AcolyteDeck(filename);
    
    JPanel content = new JPanel(new GridLayout(0, 3));
    
    cardPanel = showCardList();
    buttonPanel = buttonPane();
    deckPanel = new JPanel();
    viewDeck();
    
    content.add(cardPanel);
    content.add(buttonPanel);
    content.add(deckPanel);
    this.add(content);
    this.pack();
    this.setJMenuBar(createMenuBar());
    this.setVisible(true);
  }
  
  
  private JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        //, ActionEvent.ALT_MASK));
        menuBar.add(menu);
        //menu.addMenuListener(this);

        //a group of JMenuItems
        menuItem = new JMenuItem("New");
        menuItem.addActionListener(this);
        menuItem.setActionCommand("_new");
        menuItem.setMnemonic(KeyEvent.VK_N); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Create a new deck.");
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Save");
        menuItem.addActionListener(this);
        menuItem.setActionCommand("_save");
        menuItem.setMnemonic(KeyEvent.VK_S); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_S, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Create a new deck.");
        menu.add(menuItem);

        return menuBar;
    }
  
  private JPanel showCardList()
  {
    cardList = new java.awt.List();
    List<AcolyteCard> cards = AcolyteCard.cardList();
    List<String> names = new LinkedList<String>();
    JPanel list = new JPanel();
    for(AcolyteCard card : cards)
    {
      if(!names.contains(card.get("name")))
      {
        names.add(card.get("name"));
        cardList.add(card.get("name"));
      }
    }
    list.add(cardList);
    cardList.addActionListener(this);
    return list;
  }
  
  //buttons for manipulating the deck
  private JPanel buttonPane()
  {
    JPanel pane = new JPanel(new GridLayout());
    JButton addButton = new JButton("add");
    addButton.setActionCommand("_add");
    JButton deleteButton = new JButton("delete");
    deleteButton.setActionCommand("_delete");
    
    addButton.addActionListener(this);
    deleteButton.addActionListener(this);
    pane.add(addButton);
    pane.add(deleteButton);
    return pane;
  }
  
  //TODO TEST THIS
  private void viewDeck()
  {
    //JPanel deck = new JPanel();
    if(null != deckList)
    {
      deckPanel.remove(deckList);
    }
    deckList = new java.awt.List();
    deckList.addActionListener(this);
    
    List<String> names = new LinkedList<String>();
    for(AcolyteCard card : currentDeck.getLibrary())
    {
      if(!names.contains(card.get("name")))
      {
        deckList.add(card.get("name")+"x" + currentDeck.count(card));
        names.add(card.get("name"));
      }
    }
    deckPanel.add(deckList);
    //return deck;
  }
  
  public static void main(String[] args)
  {
    DeckEditor edit = new DeckEditor("ratDeck.xml");
  }
  
  public void actionPerformed(ActionEvent e)
  {
    //System.out.println(e.paramString());
    //System.out.println(e);
    System.out.println(e.getActionCommand());
    if("_add" == e.getActionCommand())
    {
      System.out.println("add clicked:" + cardList.getSelectedItem());
      String name = cardList.getSelectedItem();
      if(null != name)
      {
        AcolyteCard card = new AcolyteCard(name);
        currentDeck.add(card);
        //deckPanel = new JPanel();
        viewDeck();
        this.pack();
      }
    }
    else if("_delete" == e.getActionCommand())
    {
      System.out.println("delete clicked" + deckList.getSelectedItem());
      String name = deckList.getSelectedItem();
      
      if(null != name)
      {
        //name is card name plus x?? where ?? is the number occuring in the deck
        int end = name.lastIndexOf('x');
        name = name.substring(0, end);
        System.out.println(name);
        AcolyteCard card = new AcolyteCard(name);
        currentDeck.remove(card);
        viewDeck();
        this.pack();
      }
    }
    else if(e.getSource() == cardList)
    {
      System.out.println("card in card list clicked");
      AcolyteCard card = new AcolyteCard(e.getActionCommand());
      template.show(card);
    }
    else if(e.getSource() == deckList)
    {
      System.out.println("card in deck list clicked");
      String name = e.getActionCommand();
      //name is card name plus x?? where ?? is the number occuring in the deck
      int end = name.lastIndexOf('x');
      name = name.substring(0, end);
      System.out.println(name);
      AcolyteCard card = new AcolyteCard(name);
      template.show(card);
    }
    else if("_new" == e.getActionCommand())
    {
      //TODO: Destroy the current DeckEditor
      this.setVisible(false);
      new DeckEditor();
    }
    else if("_save" == e.getActionCommand())
    {
      //Get a file dialog
      JFileChooser fc = new JFileChooser();
      int returnVal = fc.showOpenDialog(new JFrame());
      
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        currentDeck.save(file);
        //This is where a real application would open the file.
        //log.append("Opening: " + file.getName() + "." + newline);
      }
    }
    else
    {
      System.out.println(e);
      System.out.println("Unknown event occured:");
      
    }
  }
}
